package server;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

import link.Keyboard;

class Control {
	DatagramSocket s;
	int port;
	String host;

	public Control(String host) {
		port = 11111;
		this.host = host;

		try {
			s = new DatagramSocket();
		} catch (SocketException e) {
			System.out.println(e);
			System.exit(-1);
		}

	}

	public Control(String host, int port) {
		this.port = port;
		this.host = host;
		try {
			s = new DatagramSocket();
		} catch (SocketException e) {
			System.out.println(e);
			System.exit(-1);
		}

	}

	void send(String msg) {
		byte[] buf = msg.getBytes();
		try {
			DatagramPacket pack = new DatagramPacket(buf, buf.length,
					InetAddress.getByName(host), port);

			s.send(pack);
		} catch (UnknownHostException e) {
			System.out.println(e);
			System.exit(-1);
		} catch (IOException e) {
			System.out.println(e);
			System.exit(-1);
		}
	}

	public static void main(String[] args) {
		Control s = new Control(args[0]);
		System.out.println("enter \"help\" for a list of the commands");
		System.out.println("(note: command output is on the server side)");
		while (true) {
			System.out.print("> ");
			StringTokenizer st = new StringTokenizer(Keyboard.readString(), " ");
			String cmd = st.nextToken();
			if (cmd.equals("help")) {
				System.out.println("\ttopo : print the topology");
				System.out
						.println("\taddroute A B : add a link between A and B");
				System.out
						.println("\tdelroute A B : remove a link between A and B");
				System.out
						.println("\trandomtopo : start the random degradation/restoration of the topology");
				System.out
						.println("\tstoprandomtopo : stop the random degradation/restoration of the topology");
				System.out.println("\texit : exit");
			} else if (cmd.equals("topo")) {
				s.send("server:dummy:topo");
			} else if (cmd.equals("exit")) {
				break;
			} else if (cmd.equals("addroute")) {
				String from = st.nextToken();
				String to = st.nextToken();
				String msg = "server:dummy:addroute:" + to + ":" + from + ":";
				s.send(msg);
			} else if (cmd.equals("delroute")) {
				String from = st.nextToken();
				String to = st.nextToken();
				String msg = "server:dummy:delroute:" + to + ":" + from + ":";
				s.send(msg);
			} else if (cmd.equals("randomtopo")) {
				s.send("server:dummy:random");
			} else if (cmd.equals("stoprandomtopo")) {
				s.send("server:dummy:stoprandom");
			}
			try {
				Thread.sleep(150);
			} catch (Exception e) {
			}
		}

		System.exit(0);
	}
}